package com.nondo.rdp.finance.service.impl.circle;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.criteria.Predicate;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.nondo.rdp.auth.common.UserUtils;
import com.nondo.rdp.auth.domain.User;
import com.nondo.rdp.auth.endpoint.model.Page;
import com.nondo.rdp.auth.model.UserBeanInner;
import com.nondo.rdp.core.common.StatusConstant;
import com.nondo.rdp.core.domain.attach.Attach;
import com.nondo.rdp.core.model.AttachBean;
import com.nondo.rdp.finance.common.DateProcessUtils;
import com.nondo.rdp.finance.domain.circle.Circle;
import com.nondo.rdp.finance.domain.circle.CircleComment;
import com.nondo.rdp.finance.domain.circle.CircleUp;
import com.nondo.rdp.finance.model.circle.CircleBean;
import com.nondo.rdp.finance.model.circle.CircleCommentBean;
import com.nondo.rdp.finance.model.circle.CircleQuery;
import com.nondo.rdp.finance.model.circle.CircleUpBean;
import com.nondo.rdp.finance.repository.circle.CircleRepository;
import com.nondo.rdp.finance.repository.circle.CircleUpRepository;
import com.nondo.rdp.finance.service.circle.CircleService;

@Transactional
@Service
public class CircleServiceImpl implements CircleService {
    @Autowired
    CircleRepository circleRepository;
    
    @Autowired
    CircleUpRepository circleUpRepository;

    @Override
    public Circle findOne(Long id) {
        return this.circleRepository.findOne(id);
    }

    @Override
    public Circle save(Circle circle) {
        return this.circleRepository.save(circle);
    }

    @Override
    public void update(Circle circle) {
        this.circleRepository.save(circle);
    }

    @Override
    public void delete(Long id) {
        this.circleRepository.deleteById(id);
    }

	@Override
	public Page<Circle> findAll(Page<Circle> page, Circle circle) {
		return null;
	}

	@Override
	public Page<Circle> findAll(Page<Circle> page, CircleBean circleBean) {
        Sort sort = new Sort(Sort.Direction.DESC, "createDate");
        PageRequest pageRequest = new PageRequest(page.getPageNumber(), page.getPageSize(), sort);
        org.springframework.data.domain.Page<Circle> dictPage = circleRepository.findAll((root, query, builder) -> {

            List<Predicate> predicates = new ArrayList<>();
            if (circleBean != null) {
            	if (circleBean.getUid() != null) {
	                predicates.add(builder.equal(root.get("user").get("id"), circleBean.getUid()));
	            }
	            if (StringUtils.isNotEmpty(circleBean.getBody())) {
	                predicates.add(builder.like(root.get("body"), "%" + circleBean.getBody() + "%"));
	            }
            }
            predicates.add(builder.like(root.get("delFlag"), StatusConstant.DEL_FLAG_NORMAL));
            Predicate[] predicatesArray = new Predicate[predicates.size()];
            predicates.toArray(predicatesArray);
            return builder.and(predicatesArray);

        }, pageRequest);

        return new Page<Circle>(dictPage);
    }

	@Override
	public CircleQuery getCircleDetail(Circle circleDb) {
		CircleQuery cq = new CircleQuery();
		cq.setId(circleDb.getId());
		cq.setBody(circleDb.getBody());
		String publishDate = DateProcessUtils.showTimeText(circleDb.getCreateDate());
		cq.setPublishDate(publishDate);
		User user = circleDb.getUser();
		UserBeanInner userBean = UserBeanInner.covert2Bean(user);
		cq.setUser(userBean);
		List<CircleComment> comments = circleDb.getComments();
		List<CircleCommentBean> commentBeans = CircleCommentBean.covert2BeanList(comments);
		cq.setComments(commentBeans);
		List<CircleUp> ups = circleDb.getUps();
		List<CircleUpBean> upBeans = CircleUpBean.covert2BeanList(ups);
		cq.setUps(upBeans);
		List<Attach> attachList = circleDb.getAttachList();
		List<AttachBean> attachBeans = AttachBean.covert2BeanList(attachList);
		cq.setAttachList(attachBeans);
		cq.setCommentNum(comments.size());
		cq.setUpNum(ups.size());
		User loginUser = UserUtils.checkUser();
		if (loginUser != null && loginUser.getId() != null) {
			CircleUp circleUp = circleUpRepository.findCircleUpByCircleAndUser(
					circleDb.getId(), loginUser.getId());
			if (circleUp != null) {
				cq.setIsUp(1);
			}
		}
		return cq;
	}
}